home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / util / wb / boxes.lha / boxes / boxes.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-31  |  4.2 KB  |  205 lines

  1. /*************************
  2.  *        Boxes.C        *
  3.  * by Tobias Ferber 1989 *
  4.  *************************/
  5.  
  6. #include <exec/types.h>
  7. #include <exec/ports.h>
  8. #include <exec/memory.h>
  9. #include <exec/libraries.h>
  10. #include <libraries/dos.h>
  11. #include <graphics/gfxbase.h>
  12. #include <intuition/intuition.h>
  13. #include <intuition/intuitionbase.h>
  14.  
  15. #include <stdio.h>
  16. #include <time.h>
  17.  
  18. #ifdef USE_TIMER_DEVICE
  19. #include "timer.h"
  20. struct timerequest *tr= (struct timerequest *)NULL;
  21. struct MsgPort *tp;  /* tr's reply port */
  22. #define MICROSPEED 5L  /* call PaintRect() every ... micro seconds */
  23. #endif
  24.  
  25. struct NewWindow nw= {
  26.   0,0,200,100,-1,-1,
  27.   CLOSEWINDOW
  28.  
  29. #ifndef USE_TIMER_DEVICE
  30.   | INTUITICKS
  31. #endif
  32.  
  33.   | NEWSIZE,
  34.   WINDOWCLOSE|WINDOWDEPTH|WINDOWDRAG|WINDOWSIZING|RMBTRAP|NOCAREREFRESH|ACTIVATE,
  35.   NULL,NULL,(UBYTE *)"Boxes",
  36.   NULL,NULL,
  37.   0,0,-1,-1,
  38.   WBENCHSCREEN
  39. };
  40.  
  41. struct IntuitionBase *IntuitionBase;
  42. struct GfxBase *GfxBase;
  43. struct Window *w;
  44. short xmin, xmax, ymin, ymax, pens;
  45.  
  46. void RethinkWindowDimensions(void)
  47. { xmin= w->BorderLeft;
  48.   xmax= w->Width - w->BorderRight;
  49.   ymin= w->BorderTop +1;
  50.   ymax= w->Height - w->BorderBottom;
  51.   pens= 1L << w->WScreen->RastPort.BitMap->Depth;
  52. }
  53.  
  54. void CloseStuff(void)
  55. {
  56.   if(tr)
  57.   { purge_timer(tr);
  58.     close_timer(tr);
  59.   }
  60.  
  61.   if(w)
  62.   { CloseWindow(w);
  63.     w= (struct Window *)NULL;
  64.   }
  65.  
  66.   if(IntuitionBase) CloseLibrary(IntuitionBase);
  67.   if(GfxBase) CloseLibrary(GfxBase);
  68. }
  69.  
  70. int OpenStuff(void)
  71. { struct Screen s;
  72.  
  73.   GfxBase= (struct GfxBase *)OpenLibrary("graphics.library",0L);
  74.   if(!GfxBase) return 1;
  75.   IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0L);
  76.   if(!IntuitionBase) return 2;
  77.   if(GetScreenData(&s,sizeof(struct Screen),WBENCHSCREEN,NULL))
  78.   {
  79.     /* GetScreenData() lies about dimensions */
  80.     s.Width= s.ViewPort.DWidth;
  81.     s.Height= s.ViewPort.DHeight;
  82.  
  83.     if((nw.LeftEdge= s.MouseX - nw.Width/2) < 0)
  84.       nw.LeftEdge= 0;
  85.     else if(nw.LeftEdge + nw.Width > s.Width)
  86.       nw.LeftEdge= s.Width - nw.Width;
  87.  
  88.     if((nw.TopEdge= s.MouseY - nw.Height/2) < 0)
  89.       nw.TopEdge= 0;
  90.     else if(nw.TopEdge+nw.Height > s.Height)
  91.       nw.TopEdge= s.Height - nw.Height;
  92.   }
  93.   else return 3;
  94.  
  95.   w= (struct Window *)OpenWindow(&nw);
  96.  
  97.   if(!w)
  98.     return 4;
  99.  
  100.   RethinkWindowDimensions();
  101.  
  102. #ifdef USE_TIMER_DEVICE
  103.   tr= open_timer(NULL,0L);
  104.  
  105.   if(!tr)
  106.     return 7;
  107.  
  108.   tp= tr->tr_node.io_Message.mn_ReplyPort;
  109.   queue_timer(tr,0,1);
  110. #endif /* USE_TIMER_DEVICE */
  111.  
  112.   return 0;
  113. }
  114.  
  115. void PaintRect(void)
  116. {
  117.   register long x0,y0,x1,y1,p,t;
  118.  
  119.   time(&t);
  120.   srand(t*270970*rand(42));
  121.  
  122.   x0= xmin + ( ((t*rand(t)) & 0x7FFF) % (xmax-xmin-4) );
  123.   y0= ymin + ( ((x0*rand(x0)) & 0x7FFF ) % (ymax-ymin-4) );
  124.  
  125.   x1= x0+1 + ( ((y0*rand(y0)) & 0x7FFF ) % (xmax-x0-2) );
  126.   y1= y0+1 + ( ((x1*rand(x1)) & 0x7FFF ) % (ymax-y0-2) );
  127.  
  128.   p= ((t+(y1*rand(y1))) & 0x7F) % pens;
  129.  
  130.   /*printf("[%d] (%d,%d) - (%d,%d)\n",p, x0,y0, x1,y1);*/
  131.  
  132.   SetAPen(w->RPort, (UBYTE)p);
  133.   RectFill(w->RPort, (SHORT)x0,(SHORT)y0, (SHORT)x1,(SHORT)y1);
  134. }
  135.  
  136. BOOL HandleIDCMP()
  137. {
  138.   BOOL done= FALSE;
  139.   struct IntuiMessage *imsg;
  140.   ULONG class;
  141.   long usersigmask  = 1L << w->UserPort->mp_SigBit,
  142.        breaksigmask = SIGBREAKF_CTRL_C,
  143.  
  144. #ifdef USE_TIMER_DEVICE
  145.        timersigmask = 1L << tp->mp_SigBit,
  146.        sig= Wait(usersigmask | breaksigmask | timersigmask);
  147. #else
  148.        sig= Wait(usersigmask | breaksigmask);
  149. #endif
  150.  
  151.   done |= (sig & breaksigmask);
  152.  
  153. #ifdef USE_TIMER_DEVICE
  154.   if(sig & timersigmask)
  155.   { while( GetMsg(tp) )
  156.       ;
  157.     PaintRect();
  158.     if(CheckIO((struct IORequest *)&tr->tr_node))
  159.       queue_timer(tr,0,MICROSPEED);
  160.   }
  161. #endif
  162.  
  163.   if(sig & usersigmask)
  164.   { while(imsg=(struct IntuiMessage *)GetMsg(w->UserPort))
  165.     { class = imsg->Class;
  166.       ReplyMsg(imsg);
  167.  
  168.       switch(class)
  169.       {
  170.         case CLOSEWINDOW:
  171.           done= TRUE;
  172.           break;
  173.         case NEWSIZE:
  174.           SetRast(w->RPort,0L);
  175.           RefreshWindowFrame(w);
  176.           RethinkWindowDimensions();
  177.           break;
  178.  
  179. #ifndef USE_TIMER_DEVICE
  180.         case INTUITICKS:
  181.           PaintRect();
  182.           break;
  183. #endif
  184.       }
  185.     }
  186.   }
  187.   return done;
  188. }
  189.  
  190. void main(void)
  191. {
  192.   int err= OpenStuff();
  193.  
  194.   if(!err)
  195.   { while( !HandleIDCMP() )
  196.       ;
  197.     CloseStuff();
  198.   }
  199.   exit(err);
  200. }
  201.  
  202. #ifdef _DCC
  203. wbmain(void) { main(); }
  204. #endif
  205.